package com.example.minefieldkeepercloud.activity.staffmanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.bean.FactoryPickerBean;
import com.example.minefieldkeepercloud.bean.StaffBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.TimeUtil;
import com.example.minefieldkeepercloud.util.TitleControl;
import com.example.minefieldkeepercloud.util.ToastUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加员工
 */
public class AddStaffActivity extends AppCompatActivity {

    @BindView(R.id.tv_factoryselect)
    TextView tv_factoryselect;

    @BindView(R.id.tv_starttime)
    TextView tv_starttime;//开始时间
    @BindView(R.id.tv_endtime)
    TextView tv_endtime;//结束时间

    private OptionsPickerView factotyPickerView;//矿场选择器
    private int factoryId;//矿场id

    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private TimePickerView pvTimeDay;//年月日时间选择器
    private int startOrEnd=0;//1 开始时间 2 结束时间
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_staff);

        ButterKnife.bind(this);
        initTitle();
        getMineFactory();
        initPvTimeYear();
    }

    /**
     * 获得矿场数据
     */
    private void getMineFactory() {
        if (NetWorkUtil.check(this)) {


            NetHelperNew.GetFactoryByCustomerId(new HttpUtils.HttpCallback() {

                @Override
                public void onSuccess(String data) {

                    FactoryPickerBean factoryPickerBean = MyGson.getInstance().fromJson(data, FactoryPickerBean.class);
                    if (factoryPickerBean.getType()==1)
                    {
                      initfactotyPickerView(factoryPickerBean.getData());
                    }

                }

            });
        }

    }


    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("添加员工");

    }
    /**
     * 年月日时分时间选择器
     */
    private void initPvTimeYear() {
        String nowdate = ymd.format(new Date(System.currentTimeMillis()));
        tv_starttime.setText(nowdate);
        tv_endtime.setText(nowdate);
        //时间选择器
        pvTimeDay = new TimePickerView(this, TimePickerView.Type.ALL);
        //设置范围
        pvTimeDay.setRange(2017, 2030);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                if (startOrEnd==1)
                {
                    tv_starttime.setText(ymd.format(date));
                }else  if (startOrEnd==2)
                {
                    tv_endtime.setText(ymd.format(date));
                }

            }


        });
    }

    @OnClick({R.id.ll_Factoryselect,R.id.tv_starttime,R.id.tv_endtime})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ll_Factoryselect:
                //选择矿场
                if (factotyPickerView!=null){
                    factotyPickerView.show();
                }else {
                    getMineFactory();
                }

                break;
            case R.id.tv_starttime:
                //开始时间
                startOrEnd=1;
                //设置选择时间
                pvTimeDay.setTime(getFormatDate(tv_starttime.getText().toString()));
                pvTimeDay.show();
                break;
            case R.id.tv_endtime:
                //结束时间
                startOrEnd=2;
                //设置选择时间
                pvTimeDay.setTime(getFormatDate(tv_endtime.getText().toString()));
                pvTimeDay.show();
                break;
        }
    }

    /**
     * 选择矿场
     */
    private void initfactotyPickerView(List<FactoryPickerBean.DataBean> list) {
        factotyPickerView = new OptionsPickerView(this);
        final ArrayList<FactoryPickerBean.DataBean> options1Items = new ArrayList<>();
        options1Items.addAll(list);

        factotyPickerView.setPicker(options1Items);

        //循环
        factotyPickerView.setCyclic(false);
        factotyPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                factoryId = options1Items.get(options1).getID();
                tv_factoryselect.setText(options1Items.get(options1).getFactoryName());
            }
        });
    }
    /**
     * 转化时间
     * @param s
     * @return
     */
    private Date getFormatDate(String s) {
        try {
            return ymd.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
